package com.bajiao.app.controller.store;

import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.aspect.annotation.Idempotent;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.dto.store.StoreApplyRequest;
import com.bajiao.dto.store.StoreApplyStep1Request;
import com.bajiao.dto.store.StoreDetailsRequest;
import com.bajiao.dto.store.StoreRequest;
import com.bajiao.entity.store.Store;
import com.bajiao.entity.store.StoreApply;
import com.bajiao.service.store.StoreApplyService;
import com.bajiao.service.store.StoreService;
import com.bajiao.vo.store.StoreDetailsResponse;
import com.bajiao.vo.store.StoreResponse;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "店铺接口")
@RestController
@RequestMapping("/api/v1/store")
public class RestStoreController extends AppBaseController {

    @Resource
    private StoreService storeService;
    @Resource
    private StoreApplyService applyService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "店铺分页列表")
    public RestResult<TableDataInfo<StoreResponse>> getStorePage(StoreRequest request) {
        if (StrUtil.hasBlank(request.getLongitude(), request.getLatitude()))
            throw new DataHandleException("未获取到经纬度信息");
        startPage();
        List<StoreResponse> storeList = storeService.getStoreList(request);
        return success(getDataTable(storeList));
    }

    @GetMapping(value = "/details")
    @ApiOperation(value = "店铺详情")
    @Idempotent(lockName = "getStoreDetails")
    public RestResult<StoreDetailsResponse> getStoreDetails(@ModelAttribute("userId") String userId,
                                                            StoreDetailsRequest request) {
        if (StrUtil.hasBlank(request.getLongitude(), request.getLatitude()))
            throw new DataHandleException("未获取到位置信息");
        StoreDetailsResponse response = storeService.getStoreDetails(userId, request);
        return success(response);
    }

    @PostMapping(value = "/verify/step1")
    @ApiOperation(value = "验证入驻第一步参数")
    public RestResult<StoreApplyStep1Request> verifyStep1(@ModelAttribute("userId") String userId,
                                                          @Validated StoreApplyStep1Request request) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        if (request.getInsideImages().split(StringPool.COMMA).length <= _Whole.FIVE)
            throw new DataHandleException("请上传五张以上店内照片");
        Integer count = storeService.lambdaQuery().eq(Store::getTitle, request.getTitle()).count();
        if (count > 0)
            throw new DataHandleException(request.getTitle() + "店铺名称重复");
        return success(request);
    }

    @PostMapping(value = "/apply")
    @ApiOperation(value = "提交入驻")
    public RestResult apply(@ModelAttribute("userId") String userId,
                            @Validated StoreApplyRequest request) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        Integer count = storeService.lambdaQuery().eq(Store::getTitle, request.getTitle()).count();
        if (count > 0)
            throw new DataHandleException(request.getTitle() + "店铺名称重复");
        applyService.apply(userId, request);
        return success();
    }

    @PostMapping(value = "/apply/details")
    @ApiOperation(value = "获取入驻申请详情")
    public RestResult<StoreApply> getApplyDetails(@ModelAttribute("userId") String userId) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        StoreApply storeApply = applyService.getByUserId(userId);
        return success(storeApply);
    }
}
